package com.itSession.cn.controller;

import com.itSession.cn.entity.*;
import com.itSession.cn.mapper.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Api(tags = "")
@Controller
@RequestMapping("/admin")
public class AdminPageController {


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private CircleInfoMapper circleInfoMapper;


    @Autowired
    private VideoInfoMapper videoInfoMapper;
    @Autowired
    private ClassInfoMapper classInfoMapper;
    @Autowired
    private LaboratoryInfoMapper laboratoryInfoMapper;
    @Autowired
    private ScoreInfoMapper scoreInfoMapper;
    @Autowired
    private NewsInfoMapper newsInfoMapper;


    /*****newsInfo*****/
    @GetMapping("/newsInfo")
    public String newsInfoPage(HttpServletRequest request) {
        request.setAttribute("path", "newsInfo");
        return "admin/newsInfo-list";
    }

    @GetMapping("/newsInfo/edit")
    public String newsInfoEdit(HttpServletRequest request) {
        request.setAttribute("path", "newsInfo");
        return "admin/newsInfo-edit";
    }

    @GetMapping("/newsInfo/edit/{userId}")
    public String newsInfoEdit(HttpServletRequest request, @PathVariable("userId") int userId) {
        NewsInfo byId = newsInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "newsInfo");
        return "admin/newsInfo-edit";
    }


    /*****scoreInfo*****/
    @GetMapping("/scoreInfo")
    public String scoreInfoPage(HttpServletRequest request) {
        request.setAttribute("path", "scoreInfo");
        return "admin/scoreInfo-list";
    }

    @GetMapping("/scoreInfo/edit")
    public String scoreInfoEdit(HttpServletRequest request) {
        request.setAttribute("path", "scoreInfo");
        request.setAttribute("users", userInfoMapper.listByRole(2));
        return "admin/scoreInfo-edit";
    }

    @GetMapping("/scoreInfo/edit/{userId}")
    public String scoreInfoEdit(HttpServletRequest request, @PathVariable("userId") int userId) {
        ScoreInfo byId = scoreInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "scoreInfo");
        request.setAttribute("users", userInfoMapper.listByRole(2));
        return "admin/scoreInfo-edit";
    }


    /*****laboratoryInfo*****/
    @GetMapping("/laboratoryInfo")
    public String laboratoryInfoPage(HttpServletRequest request) {
        request.setAttribute("path", "laboratoryInfo");
        return "admin/laboratoryInfo-list";
    }

    @GetMapping("/laboratoryInfo/edit")
    public String laboratoryInfoEdit(HttpServletRequest request) {
        request.setAttribute("path", "laboratoryInfo");
        request.setAttribute("roles", classInfoMapper.listAll());
        return "admin/laboratoryInfo-edit";
    }

    @GetMapping("/laboratoryInfo/edit/{userId}")
    public String laboratoryInfoEdit(HttpServletRequest request, @PathVariable("userId") int userId) {
        LaboratoryInfo byId = laboratoryInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "laboratoryInfo");
        request.setAttribute("roles", classInfoMapper.listAll());
        return "admin/laboratoryInfo-edit";
    }


    /*****classInfo*****/
    @GetMapping("/classInfo")
    public String classInfoPage(HttpServletRequest request) {
        request.setAttribute("path", "classInfo");
        return "admin/classInfo-list";
    }

    @GetMapping("/classInfo/edit")
    public String classInfoEdit(HttpServletRequest request) {
        request.setAttribute("path", "classInfo");
        return "admin/classInfo-edit";
    }

    @GetMapping("/classInfo/edit/{userId}")
    public String classInfoEdit(HttpServletRequest request, @PathVariable("userId") int userId) {
        ClassInfo byId = classInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "classInfo");
        return "admin/classInfo-edit";
    }



    /*****videoInfo*****/
    @GetMapping("/videoInfo")
    public String videoInfoPage(HttpServletRequest request) {
        request.setAttribute("path", "videoInfo");
        return "admin/videoInfo-list";
    }

    @GetMapping("/videoInfo/edit")
    public String videoInfoEdit(HttpServletRequest request) {
        request.setAttribute("path", "videoInfo");
        return "admin/videoInfo-edit";
    }

    @GetMapping("/videoInfo/edit/{userId}")
    public String videoInfoEdit(HttpServletRequest request, @PathVariable("userId") int userId) {
        VideoInfo byId = videoInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "videoInfo");
        return "admin/videoInfo-edit";
    }




    /*****circleInfo*****/
    @GetMapping("/circleInfo")
    public String circleInfoPage(HttpServletRequest request) {
        request.setAttribute("path", "circleInfo");
        return "admin/circleInfo-list";
    }

    @GetMapping("/circleInfo/edit")
    public String circleInfoEdit(HttpServletRequest request) {
        request.setAttribute("path", "circleInfo");
        return "admin/circleInfo-edit";
    }

    @GetMapping("/circleInfo/edit/{userId}")
    public String circleInfoEdit(HttpServletRequest request, @PathVariable("userId") int userId) {
        CircleInfo byId = circleInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "circleInfo");
        return "admin/circleInfo-edit";
    }


    /*****userInfo*****/
    @GetMapping("/userInfo")
    public String usersPage(HttpServletRequest request) {
        request.setAttribute("path", "userInfo");
        return "admin/userInfo-list";
    }

    @GetMapping("/userInfo/edit")
    public String editUser(HttpServletRequest request) {
        request.setAttribute("path", "userInfo");

        return "admin/userInfo-edit";
    }

    @GetMapping("/userInfo/edit/{userId}")
    public String editUser(HttpServletRequest request, @PathVariable("userId") int userId) {
        UserInfo byId = userInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "userInfo");
        return "admin/userInfo-edit";
    }

    /*****userInfo*****/
    @GetMapping("/userInfo2")
    public String usersPage2(HttpServletRequest request) {
        request.setAttribute("path", "userInfo2");
        return "admin/userInfo-list2";
    }

    @GetMapping("/userInfo2/edit")
    public String editUser2(HttpServletRequest request) {
        request.setAttribute("path", "userInfo2");
        return "admin/userInfo-edit2";
    }

    @GetMapping("/userInfo2/edit/{userId}")
    public String editUser2(HttpServletRequest request, @PathVariable("userId") int userId) {
        UserInfo byId = userInfoMapper.getById(userId);
        request.setAttribute("goods", byId);
        request.setAttribute("path", "userInfo2");
        return "admin/userInfo-edit2";
    }



    @GetMapping("/profile")
    public String profile(HttpServletRequest request) {
        String userName = (String) request.getSession().getAttribute("loginUserId");
        UserInfo userByAccount = userInfoMapper.getUserByAccount(userName);
        if (userByAccount == null) {
            return "admin/login";
        }
        request.setAttribute("path", "profile");
        request.setAttribute("adminUserInfo", userByAccount);
        return "admin/profile";
    }


    @GetMapping({"/login"})
    public String login() {
        return "admin/login";
    }

    @GetMapping({"/test"})
    public String test() {
        return "admin/test";
    }

    @GetMapping({"", "/", "/index", "/index.html"})
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "index");
        return "admin/index";
    }

    @PostMapping(value = "/login")
    public String login(@RequestParam("userAccount") String userAccount,
                        @RequestParam("userPwd") String userPwd,
                        HttpSession session) {
        if (StringUtils.isEmpty(userAccount) || StringUtils.isEmpty(userPwd)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "admin/login";
        }
        UserInfo user = userInfoMapper.getUserByAccount(userAccount);
        if (user != null) {
            if (!userPwd.equals(user.getUserPwd())) {
                session.setAttribute("errorMsg", "账号或者密码错误");
                return "admin/login";
            }
            session.setAttribute("loginUser", user.getUserName());
            session.setAttribute("loginUserId", user.getUserId());
            session.setAttribute("role", user.getUserRole());
            //session过期时间设置为7200秒 即两小时
            session.setMaxInactiveInterval(60 * 60 * 2);
            return "redirect:/admin/index";
        } else {
            session.setAttribute("errorMsg", "登录失败");
            return "admin/login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUserId");
        request.getSession().removeAttribute("loginUser");
        request.getSession().removeAttribute("errorMsg");
        return "admin/login";
    }

}
